<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



require_once(APPPATH .'/libraries/REST_Controller.php');



/**
 * class Location
 * Will be the controller for all crown actions within the vta application.
 *
 * @author      Jeroen Sen <jeroensen@22vision.nl>
 * @version     20121105
 *
 */
class Location extends REST_Controller {



    /**
     * public function create_put
     * Will add another Location to the database based on a PUT request.
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20121105
     * @param       void, working of the member Array $this->put
     * @return      String true
     *
     */
    public function create_put()
    {
error_log(__METHOD__);
        $location = new Entities\Location;
error_log($this->put('longitude'));
error_log($this->put('latitude'));
        $location->setLongitude($this->put('longitude'));
        $location->setLatitude($this->put('latitude'));
        $location->setStreet($this->put('street'));
        $location->setCity($this->put('city'));
        $location->setPostalcode($this->put('postalcode'));
        $location->setHousenumber($this->put('housenumber'));

        // the recieved value for a country isn't the id but the abbreviation,
        // change this for the id
        $criteria = array();
        $orderBy = array('code' => 'ASC');

        $countryName = $this->put('country');
error_log($countryName);
        if (isset($countryName) && is_string($countryName) && strlen(trim($countryName)) > 0) {
            $criteria['name'] = $countryName;
        }

//        $countries = $this->doctrine->em->getRepository('Entities\Country')->findBy($criteria, $orderBy);
        $country = $this->doctrine->em->getRepository('Entities\Country')->findOneBy($criteria);


//        foreach ($countries as $index => $country) {
//            break;
//        }

        $location->setCountry($country);

        $this->doctrine->em->persist($location);
		$this->doctrine->em->flush();

		$data = array('id' => $location->getId(),
		              'longitude' => $location->getLongitude(),
		              'latitude' => $location->getLatitude(),
		              'street' => $location->getStreet(),
		              'city' => $location->getCity(),
		              'postalcode' => $location->getPostalcode(),
		              'housenumber' => $location->getHousenumber());

        $this->response(array('data' => $data));

    } // public function create_put()



    /**
     * public function update_post
     * Will update a single Crown object based on a POST request. Works of the member variable Array $this->post.
     * curl -X POST -H "Accept: application/json" -d '{"id":666}' http://zichtopbomen.local/vta/crown/edit
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120808
     * @param       void
     * @return      String true
     *
     */
    public function update_post()
    {
        $id = intval($this->post('id'));
        $active = false;

        if ($this->post('active') == 'true') {
            $active = true;
        }


        $crown = $this->doctrine->em->find('Entities\Crown', $id);

        $crown->setName($this->post('name'));
        $crown->setValue($this->post('value'));
        $crown->setActive($active);

        $persist = $this->doctrine->em->persist($crown);
		$this->doctrine->em->flush();

        $this->response('true');

    } // public function update_post()



    /**
     *
     *
     */
    public function tab_get()
    {
error_log(__METHOD__);
        $this->load->view('vta/crown/tab');

    } //public function test_get()




} // class Vta extends CI_Controller